package com.xhu.controller;

import com.alibaba.fastjson.JSON;
import com.xhu.common.ResponseInfo;
import com.xhu.common.vo.EmpInfoVo;
import com.xhu.constant.SessionConstant;
import com.xhu.exceptions.UserLoginException;
import com.xhu.pojo.EmpInfo;
import com.xhu.pojo.User;
import com.xhu.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Encoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import static com.xhu.constant.ResultConstant.SUCCESS;

@RestController

@RequestMapping("user")
public class UserController extends MyBaseController {
    @Autowired()
    IUserService IUserService;

    /**
     * 用户登录
     */
    @PostMapping("/login")
    public String login(String id, String password, HttpServletRequest request) {

        //实现登录逻辑
        ResponseInfo<Map<String, Object>> res = IUserService.login(id, password);

        //存放用户名至session
        request.getSession().setAttribute(SessionConstant.ID, id);

        return JSON.toJSONString(res);

    }

    @PostMapping("/getname")
    public String getname(@RequestBody String formId) {
        String id = JSON.parseObject(formId, Map.class).get("id").toString();
        String nameById = IUserService.getNameById(id);

        ResponseInfo<String> res = new ResponseInfo<String>()
                .setMsg("查询成功")
                .setStatus(SUCCESS);

        res.setData(nameById);

        return JSON.toJSONString(res);
    }

    @PostMapping("/getAllUserInfo")
    public String getAllUserInfo() {
        ResponseInfo<List<User>> res = new ResponseInfo<List<User>>()
                .setMsg("查询成功")
                .setStatus(SUCCESS);

        List<User> map = IUserService.getAllIdAndName();
        res.setData(map);
        return JSON.toJSONString(res);

    }

    @PostMapping("/getInfo")
    public String getInfo(@RequestBody String id) {
        String idStr = this.getSingleStr(id, "id");

        EmpInfoVo infoById = IUserService.getInfoById(idStr);
        return this.returnSuccessWithData("查询成功", infoById);
    }

    @PostMapping("/updateEmpInfo")
    public String updateEmpInfo(@RequestBody EmpInfo entity) {
        IUserService.updateInfo(entity);
        return this.returnSuccessWithNoData("修改成功");
    }

    @PostMapping("/uploadProfilePhoto")
    public String updateProfilePhoto(@RequestBody MultipartFile file, HttpServletRequest request) {
        IUserService.uploadProfilePhoto(file, (String) request.getSession().getAttribute(SessionConstant.ID));
        return this.returnSuccessWithNoData("上传成功");
    }

    @PostMapping("/getProfilePhoto")
    public String getProfilePhoto(@RequestBody String id) {

        String idStr = this.getSingleStr(id, "id");

        String url = IUserService.getPhotoUrl(idStr);
        File file = new File(url);
        String res;
        try {
            FileInputStream is = new FileInputStream(file);
            byte[] buffer = new byte[(int) file.length()];
            is.read(buffer);
            is.close();
            res = new BASE64Encoder().encode(buffer);
        } catch (IOException e) {
            e.printStackTrace();
            throw new UserLoginException("传输失败");
        }
        return res;

    }
 /*   @PostMapping("/getProfilePhoto")
    public void getProfilePhoto(@RequestBody String id, HttpServletResponse response) throws LoginException {
        response.setHeader("Pragma", "no-cache");
        response.setContentType("image/jepg");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        String idStr = this.getSingleStr(id, "id");

        String url = IUserService.getPhotoUrl(idStr);
        try {
            InputStream is = new FileInputStream(url);
            BufferedImage read = ImageIO.read(is);
            ImageIO.write(read, "JPEG", response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
            throw new UserLoginException("传输失败");
        }

    }*/
}
